package charts ;
import charts.series.Element;
import charts.series.dots.PointDotBase;
import charts.series.dots.Point;
import charts.LineStyle;
import string.Utils;
import flash.display.BlendMode;
import flash.geom.Point;
import flash.display.Sprite;
import charts.series.dots.DefaultDotProperties;

class Area extends LineBase {
	
	public function new ( json: Dynamic ) {
		super();			
		style = /*{
			//values:			[],
			//width:			2,
			//color:			'#3030d0',
			//fill:			'#3030d0',
			//text:			'',		// <-- default not display a key
			//'dot-size':		5,
			//'font-size':	10,
			//'fill-alpha':	0.6,
			//'line-style':	new LineStyle( json['line-style'] ),
			//loop:			false,		// <-- for radar charts
			
			// default dot:
			// HACK: fix this (remove the merge below)
			//'--dot-style':	null
		};*/

		{
			values		: []
			, width		: 2
			, color	: '#3030d0'
			, fill		: '#3030d0'
			, text		: '' // <-- default not display a key
			, dot_size	: 5
			, font_size	: 10
			, fill_alpha	: 0.6
			, line_style	: new LineStyle( Reflect.field(json,'line-style') )
			, loop		: false // <-- for radar charts
			
			// default dot:
			// HACK: fix this (remove the merge below)
			, __dot_style	: String
		};
		
		ObjectHelper.merge_2( json, style );

		// hack:
		this.style.__dot_style = new DefaultDotProperties(
			Reflect.field(json,'dot-style'), '#val#', this.style.color);
		
		if( this.style.fill == '' )
			this.style.fill = this.style.color;
			
		this.style.color = string.Utils.get_color( this.style.color );
		this.style.fill = string.Utils.get_color( this.style.fill );
		
		this.key = style.text;
		this.font_size = style.font_size;
		this.values = style.values;
		this.add_values();
		
		//
		// so the mask child can punch a hole through the line
		//
		this.blendMode = BlendMode.LAYER;
	}
	
	public override function resize (sc:ScreenCoordsBase): Void {
		
		this.graphics.clear();
		// now draw the line + hollow dots
		super.resize(sc);
		
		var last:Element;
		var first:Bool = true;
		var tmp:Sprite;
		
		for ( i in 0...this.numChildren ) {
			
			tmp = cast(this.getChildAt(i),Sprite);
			
			// filter out the masks
			if(Std.is(tmp,Element )) {
				
				var e:Element = cast(tmp,Element);
				
				if( first )
				{
					
					first = false;
					
					if (this.style.loop)
					{
						// assume we are in a radar chart
						this.graphics.moveTo( e.x, e.y );
					}
					else
					{
						// draw line from Y=0 up to Y pos
						this.graphics.moveTo( e.x, sc.get_y_bottom(false) );
					}
					
					//
					// TO FIX BUG: you must do a graphics.moveTo before
					//             starting a fill:
					//
					this.graphics.lineStyle(0,0,0);
					this.graphics.beginFill( this.style.fill, this.style['fill-alpha'] );
					
					if (!this.style.loop)
						this.graphics.lineTo( e.x, e.y );
					
				}
				else
				{
					this.graphics.lineTo( e.x, e.y );
					last = e;
				}
			}
		}
		
		if ( last != null ) {
			if ( !this.style.loop) {
				this.graphics.lineTo( last.x, sc.get_y_bottom(false) );
			}
		}
		

		this.graphics.endFill();
	}
}
